package com.linkoog.devtools.code_generator.wizard.generate.database.table.dialog.datasource;

import com.linkoog.devtools.code_generator.ActionCallback;
import com.linkoog.devtools.code_generator.database.domain.ConnectionInfo;
import com.linkoog.devtools.code_generator.wizard.generate.database.table.dialog.datasource.panel.LeftPanelBuilder;
import com.linkoog.devtools.code_generator.wizard.generate.database.table.dialog.datasource.panel.RightPanelBuilder;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.ui.Splitter;
import com.intellij.util.ui.JBUI;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.beans.PropertyChangeListener;

public class DataSourceSettingDialog extends DialogWrapper {


    private JPanel myMainPanel;

    private LeftPanelBuilder leftPanelBuilder;
    private RightPanelBuilder rightPanelBuilder;

    private final ActionCallback disposeCallback;

    private Action mySaveAction;
    private Action myCanelAction;
    private Action myCloseAction;

    public static final String VISIBLE_PROPERTY = "visible_property";


    public DataSourceSettingDialog(@NotNull JComponent jComponent, ActionCallback disposeCallback) {
        super(jComponent, true);
        this.myMainPanel.setMinimumSize(JBUI.size(580, 450));
        this.myMainPanel.setPreferredSize(JBUI.size(580, 450));


        this.disposeCallback = disposeCallback;
        createMyActions();

        setTitle("配置数据源");
        //setSize(600,500);
        setResizable(false);
        initContextPanel();
        init();
    }

    private void createMyActions() {
        this.mySaveAction = new DialogAction("Save") {
            @Override
            protected void doAction(ActionEvent e) {
                if (saveAction()) {
                    Messages.showInfoMessage(myMainPanel, "保存成功！", "数据源");
                }
            }
        };
        this.mySaveAction.putValue(FOCUSED_ACTION, Boolean.TRUE);

        this.myCanelAction = new DialogAction("Cancel") {
            @Override
            protected void doAction(ActionEvent e) {
                cancelAction();
            }
        };

        this.myCloseAction = new DialogAction("Close") {
            @Override
            protected void doAction(ActionEvent e) {
                closeAction();
            }
        };

        myCloseAction.putValue(VISIBLE_PROPERTY, Boolean.TRUE);
        myCloseAction.putValue(DEFAULT_ACTION, Boolean.TRUE);
    }


    @Override
    protected void dispose() {
        super.dispose();
        if (disposeCallback != null) {
            disposeCallback.callback();
        }
    }

    private void initContextPanel() {
        leftPanelBuilder = new LeftPanelBuilder();
        rightPanelBuilder = new RightPanelBuilder();

        leftPanelBuilder.setConnectionInfoConsumer(info -> {
            rightPanelBuilder.resetContextPanel(info);
        });


        leftRightSplitPanel();
        //this.myMainPanel.add(rightPanelBuilder.getMyMainPanel(), BorderLayout.CENTER);
    }

    private void leftRightSplitPanel() {
        // 左右分割面板
        Splitter splitter = new Splitter(false, 0.28F);
        //splitter.setResizeEnabled(false);
        splitter.setFirstComponent(leftPanelBuilder.getMyMainPanel());
        splitter.setSecondComponent(rightPanelBuilder.getMyMainPanel());
        this.myMainPanel.add(splitter, BorderLayout.CENTER);
    }

    @NotNull
    protected Action[] createActions() {
        return new Action[]{mySaveAction,
                //myCanelAction,
                myCloseAction};
    }

    @NotNull
    protected Action[] createLeftSideActions() {
        return new Action[0];
    }

    /*protected JComponent createSouthPanel() {
        return null;
    }*/


    @Override
    protected JButton createJButtonForAction(Action action) {
        JButton jButton = super.createJButtonForAction(action);
        Object visibleObj = action.getValue(VISIBLE_PROPERTY);
        if (visibleObj instanceof Boolean) {
            jButton.setVisible((boolean) visibleObj);
        }
        return jButton;
    }

    public boolean doSaveAction() {
        boolean flag = rightPanelBuilder.saveConnectionInfo();
        leftPanelBuilder.getDatasourceTree().setSelectionRow(-1);
        rightPanelBuilder.resetContextPanel(null);

        return flag;
    }

    public boolean saveAction() {
        return doSaveAction();
    }

    public void cancelAction() {
        leftPanelBuilder.getDatasourceTree().setSelectionRow(-1);
        rightPanelBuilder.resetContextPanel(null);
    }

    public void closeAction() {
        ConnectionInfo info = rightPanelBuilder.getCurrentConnectionInfo();
        if (info != null) {
            int result = Messages.showYesNoDialog(info.getName(), "确认保存？", Messages.getQuestionIcon());
            if (result == Messages.YES) {
                saveAction();
            }
        }
        close(OK_EXIT_CODE);
    }


    protected abstract class DialogAction extends DialogWrapperAction {
        private DialogAction(String name) {
            super(name);
            addPropertyChangeListener(myRepaintOnNameChangeListener);
        }

        protected abstract void doAction(ActionEvent e);
    }

    private final PropertyChangeListener myRepaintOnNameChangeListener = evt -> {
        if (Action.NAME.equals(evt.getPropertyName())) {
            repaint();
        }
    };


    @Override
    @Nullable
    protected JComponent createCenterPanel() {
        return myMainPanel;
    }

    {
// GUI initializer generated by IntelliJ IDEA GUI Designer
// >>> IMPORTANT!! <<<
// DO NOT EDIT OR ADD ANY CODE HERE!
        $$$setupUI$$$();
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        myMainPanel = new JPanel();
        myMainPanel.setLayout(new BorderLayout(0, 0));
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() {
        return myMainPanel;
    }

}
